package com.example.cart_system.service.Impl;

import com.example.cart_system.mapper.GoodsMapper;
import com.example.cart_system.mapper.OrderMapper;
import com.example.cart_system.pojo.GoodsInfo;
import com.example.cart_system.pojo.Order;
import com.example.cart_system.pojo.Usr;
import com.example.cart_system.service.OrderService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    //数据格式原始形式
    @Override
    public List<Order> getOrderList(Usr usr) {
        List<Order> orderlist = orderMapper.getOrderList(usr.getUsrid());
        for (Order e : orderlist) {
            try {
                List<GoodsInfo> goodList = objectMapper.readValue(
                        e.getGoods(),
                        objectMapper.getTypeFactory().constructCollectionType(List.class, GoodsInfo.class)
                );
                List<GoodsInfo> goods = new ArrayList<>();
                for (GoodsInfo g : goodList) {
                    GoodsInfo info = goodsMapper.selectGoodByGid(g.getGid());
                    info.setCnn(g.getCnn());
                    goods.add(info);
                }
                e.setGoodsList(goods);
            } catch (Exception s) {
                log.info(s.getMessage());
            }
        }
        System.out.println("orderlist:" + orderlist);
        return orderlist;
    }

}
